home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / thesrc10.zip / DIRECTRY.C < prev    next >
C/C++ Source or Header  |  1992-08-12  |  9KB  |  301 lines

  1. /***********************************************************************/
  2. /* DIRECTRY.C - Directory routines                                     */
  3. /***********************************************************************/
  4. /*
  5.  * THE - The Hessling Editor. A text editor similar to VM/CMS xedit.
  6.  * Copyright (C) 1991,1992 Mark Hessling
  7.  *
  8.  * This program is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU General Public License as
  10.  * published by the Free Software Foundation; either version 2 of
  11.  * the License, or any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16.  * General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to:
  20.  *
  21.  *    The Free Software Foundation, Inc.
  22.  *    675 Mass Ave,
  23.  *    Cambridge, MA 02139 USA.
  24.  *
  25.  *
  26.  * If you make modifications to this software that you feel increases
  27.  * it usefulness for the rest of the community, please email the
  28.  * changes, enhancements, bug fixes as well as any and all ideas to me.
  29.  * This software is going to be maintained and enhanced as deemed
  30.  * necessary by the community.
  31.  *
  32.  * Mark Hessling                     email: M.Hessling@itc.gu.edu.au
  33.  * 36 David Road                     Phone: +61 7 849 7731
  34.  * Holland Park                      Fax:   +61 7 875 7877
  35.  * QLD 4121
  36.  * Australia
  37.  */
  38. #ifdef __OS2__
  39. #   define INCL_DOS
  40. #endif
  41. #include "the.h"
  42. #include "directry.h"
  43. #include "fnmatch.h"
  44.  
  45. #ifdef PROTO
  46. char *make_full(char *path, char *file)
  47. #else
  48. char *make_full(path, file)
  49. char *path, *file;
  50. #endif
  51. {
  52.  static char filebuf[BUFSIZ];
  53.  
  54.  if (strlen(path)+1+strlen(file)+1 > BUFSIZ)
  55.     return(NULL);
  56.  if (!strcmp(path, "") || !strcmp(path, "."))
  57.    {
  58.     (void) strcpy(filebuf, file);
  59.     return(filebuf);
  60.    }
  61.  (void) strcpy(filebuf, path);
  62.  if (path[strlen(path) - 1] != SLASH && *file != SLASH)
  63.     (void) strcat(filebuf, STR_SLASH);
  64.  (void) strcat(filebuf, file);
  65.  return(filebuf);
  66. }
  67. /*********************************************************************/
  68. #ifdef UNIX
  69. #ifdef PROTO
  70. int getfiles(char *path,char *files,struct dirfile **dpfirst,
  71.                                     struct dirfile **dplast)
  72. #else
  73. int getfiles(path,files,dpfirst,dplast)
  74. char *path;
  75. char *files;
  76. struct dirfile **dpfirst;
  77. struct dirfile **dplast;
  78. #endif
  79. {
  80.  DIR *dirp;
  81.  struct stat sp;
  82.  struct dirent *direntp;
  83.  struct dirfile *dp;
  84.  char *full_name;
  85.  int entries = 10;
  86.  
  87.  dirp = opendir(path);
  88.  if (dirp == NULL)
  89.     return(10);
  90.  
  91.  dp = *dpfirst = (struct dirfile *)calloc(entries, sizeof (struct dirfile));
  92.  if (dp == NULL)
  93.     return(30);
  94.  *dplast = *dpfirst + entries;
  95.  
  96.  for (direntp = readdir(dirp);direntp != NULL;direntp = readdir(dirp))
  97.     {
  98.      if (fnmatch(files,direntp->d_name,0) == 0)
  99.        {
  100.         if ((full_name = make_full(path,direntp->d_name)) == NULL)
  101.            return(30);
  102.         if (stat(full_name,&sp) != 0)
  103.            continue;
  104.         if ((dp->fname = (char *)calloc(strlen(direntp->d_name)+1,sizeof(char))) == NULL)
  105.            return(30);
  106.         strcpy(dp->fname,direntp->d_name);
  107.         dp->fattr = sp.st_mode;
  108.         dp->ftime = sp.st_mtime;
  109.         dp->fsize = sp.st_size;
  110.     dp->fdate = 0;
  111.         dp++;
  112.         if (dp == *dplast)
  113.           {
  114.            *dpfirst = (struct dirfile *)realloc((char *)*dpfirst,
  115.                       2 * entries * sizeof (struct dirfile));
  116.            if (*dpfirst == NULL)
  117.               return(30);
  118.            dp = *dpfirst + entries;
  119.            *dplast = dp + entries;
  120.            entries *= 2;
  121.           }
  122.        }
  123.     }
  124.  closedir(dirp);
  125.  *dplast = dp;
  126.  return(0);
  127. }
  128. #else
  129. /*********************************************************************/
  130. #ifdef PROTO
  131. int getfiles(char *path,char *files,struct dirfile **dpfirst,
  132.                                     struct dirfile **dplast)
  133. #else
  134. int getfiles(path,files,dpfirst,dplast)
  135. char *path;
  136. char *files;
  137. struct dirfile **dpfirst;
  138. struct dirfile **dplast;
  139. #endif
  140. {
  141.  struct dirfile *dp;
  142. #ifdef OS2
  143.  USHORT matches=1;
  144.  ULONG rsvrd=0;
  145.  HDIR hdir=HDIR_SYSTEM;
  146. #endif
  147.  FSTR_TYPE ffblk;
  148.  ATTR_TYPE attrs = F_RO | F_HI | F_SY | F_DI | F_AR;
  149.  DONE_TYPE done;
  150.  char *full_path;
  151.  char str_attr[11];
  152.  char str_date[10];
  153.  char str_time[6];
  154.  int entries = 10;
  155.  
  156.  if ((full_path = make_full(path,"*.*")) == NULL)
  157.        return(1);
  158.  
  159. #if defined(DOS) && defined(TC)
  160.  done = findfirst(full_path,&ffblk,attrs);
  161. #endif
  162. #if defined (DOS) && defined(MSC)
  163.  done = _dos_findfirst(full_path,attrs,&ffblk);
  164. #endif
  165. #ifdef OS2
  166.  done = DosFindFirst((PSZ) full_path, (PHDIR)&hdir, (USHORT)attrs,
  167.              (PFILEFINDBUF)&ffblk, (USHORT)sizeof(ffblk), (PUSHORT)&matches,
  168.              (ULONG)rsvrd);
  169. #endif
  170.  if (done != 0)
  171.     return(1);
  172.  
  173.  dp = *dpfirst = (struct dirfile *)calloc(entries, sizeof (struct dirfile));
  174.  *dplast = *dpfirst + entries;
  175.  
  176.  
  177.  while(!done)
  178.     {
  179.      if (fnmatch(files,ffblk.NAME_NAME,FNM_IGNORECASE) == 0)
  180.        {
  181.     if ((dp->fname = (char *)calloc(strlen(ffblk.NAME_NAME)+1,sizeof(char))) == NULL)
  182.        return(1);
  183.     strcpy(dp->fname,ffblk.NAME_NAME);
  184.     dp->fattr = ffblk.ATTR_NAME;
  185.     dp->ftime = ffblk.TIME_NAME;
  186.     dp->fsize = ffblk.SIZE_NAME;
  187.     dp->fdate = ffblk.DATE_NAME;
  188.     dp++;
  189.     if (dp == *dplast)
  190.       {
  191.            *dpfirst = (struct dirfile *)realloc((char *)*dpfirst,
  192.                       2 * entries * sizeof (struct dirfile));
  193.            if (*dpfirst == NULL)
  194.               return(1);
  195.            dp = *dpfirst + entries;
  196.            *dplast = dp + entries;
  197.            entries *= 2;
  198.           }
  199.        }
  200. #if defined(DOS) && defined(TC)
  201.      done = findnext(&ffblk);
  202. #endif
  203. #if defined(DOS) && defined(MSC)
  204.      done = _dos_findnext(&ffblk);
  205. #endif
  206. #ifdef OS2
  207.      done = DosFindNext((HDIR)hdir, (PFILEFINDBUF)&ffblk, (USHORT)sizeof(ffblk),
  208.             (PUSHORT)&matches);
  209. #endif
  210.     }
  211.  *dplast = dp;
  212.  return(0);
  213. }
  214. #endif
  215. /*********************************************************************/
  216. #ifdef PROTO
  217. int fcomp(struct dirfile *first,struct dirfile* next)
  218. #else
  219. int fcomp(first,next)
  220. struct dirfile *first;
  221. struct dirfile* next;
  222. #endif
  223. {
  224.  return(strcmp(first->fname,next->fname));
  225. }
  226. /*********************************************************************/
  227. #ifdef PROTO
  228. char *file_date(D_TYPE date,char *str_date)
  229. #else
  230. char *file_date(date,str_date)
  231. D_TYPE date;
  232. char *str_date;
  233. #endif
  234. {
  235.  static char mon[12][4] = {
  236.  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
  237.  sprintf(str_date,"%2d-%3.3s-%2.2d",DAYS_MASK,MONT_MASK,YEAR_MASK);
  238.  return(str_date);
  239. }
  240. /*********************************************************************/
  241. #ifdef PROTO
  242. char *file_time(T_TYPE time,char *str_time)
  243. #else
  244. char *file_time(time,str_time)
  245. T_TYPE time;
  246. char *str_time;
  247. #endif
  248. {
  249.  sprintf(str_time,"%2d:%2.2d",HOUR_MASK,MINU_MASK);
  250.  return(str_time);
  251. }
  252. /*********************************************************************/
  253. #ifdef PROTO
  254. char *file_attrs(ATTR_TYPE attrs,char *str_attr)
  255. #else
  256. char *file_attrs(attrs,str_attr)
  257. ATTR_TYPE attrs;
  258. char *str_attr;
  259. #endif
  260. {
  261. #ifdef UNIX
  262.  ATTR_TYPE ftype=(attrs & S_IFMT);
  263.  
  264.  str_attr[10] = '\0';
  265.  switch(ftype)
  266.    {
  267.     case S_IFDIR:  str_attr[0] = 'd'; break;
  268.     case S_IFCHR:  str_attr[0] = 'c'; break;
  269.     case S_IFSOCK: str_attr[0] = 's'; break;
  270.     case S_IFIFO:  str_attr[0] = 'p'; break;
  271.     case S_IFLNK:  str_attr[0] = 'l'; break;
  272.     default:       str_attr[0] = '-'; break;
  273.    }
  274.  str_attr[1] = (attrs & S_IRUSR) ? 'r' : '-';
  275.  str_attr[2] = (attrs & S_IWUSR) ? 'w' : '-';
  276.  str_attr[3] = (attrs & S_IXUSR) ? 'x' : '-';
  277.  str_attr[4] = (attrs & S_IRGRP) ? 'r' : '-';
  278.  str_attr[5] = (attrs & S_IWGRP) ? 'w' : '-';
  279.  str_attr[6] = (attrs & S_IXGRP) ? 'x' : '-';
  280.  str_attr[7] = (attrs & S_IROTH) ? 'r' : '-';
  281.  str_attr[8] = (attrs & S_IWOTH) ? 'w' : '-';
  282.  str_attr[9] = (attrs & S_IXOTH) ? 'x' : '-';
  283. #else
  284.  strcpy(str_attr,".... ");
  285.  if ((attrs & F_RO) == F_RO)
  286.    str_attr[0] = 'r';
  287.  if ((attrs & F_AR) == F_AR)
  288.    str_attr[1] = 'a';
  289.  if ((attrs & F_SY) == F_SY)
  290.    str_attr[2] = 's';
  291.  if ((attrs & F_HI) == F_HI)
  292.    str_attr[3] = 'h';
  293.  str_attr[4] = '\0';
  294.  if ((attrs & F_DI) == F_DI)
  295.    strcat(str_attr,"(dir) ");
  296.  else
  297.    strcat(str_attr,"      ");
  298. #endif
  299.  return(str_attr);
  300. }
  301.